package com.pxx.service.controller;

import com.pxx.item.pojo.SpecParam;
import com.pxx.item.pojo.SpectGroup;
import com.pxx.service.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/item/spec")
@CrossOrigin
public class SpecController {
    @Autowired
    SpecService specService;

    /**
     * 根据分类id查询分组
     * @param cid
     * @return
     */
    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<SpectGroup>> queryGroupsByCid(@PathVariable("cid") Long cid){
        List<SpectGroup>  groupList=  specService.queryGroupsByCid(cid) ;
        if (CollectionUtils.isEmpty(groupList)){
            return  ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(groupList);
    }

    /**
     *
     * @param gid 组id
     * @param cid 商品分类三级id
     * @param generic  是否通用
     * @param searching 是否可查询
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "gid",required = false)Long gid,
            @RequestParam(value = "cid",required = false)Long cid,
            @RequestParam(value = "generic",required = false)Boolean generic,
            @RequestParam(value = "searching",required = false)Boolean searching

    ){
        List<SpecParam>  specParamList=     specService.queryParams(gid,cid,generic,searching);
        if (CollectionUtils.isEmpty(specParamList)){
            return  ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specParamList);
    }





}
